package sol.leetcode;

import def.ListNode;
import utils.LinkedListUtils;

/**
 * Remove Duplicates from Sorted List II
 */
public class Issue082 {

    public ListNode deleteDuplicates(ListNode head) {
        if (null == head) return null;
        if (null == head.next) return head;

        int val = head.val;
        ListNode p = head.next;

        if (p.val != val) {
            head.next = deleteDuplicates(p);
            return head;
        } else {
            while (p != null && p.val == val) p = p.next;
            return deleteDuplicates(p);
        }
    }

    public static void main(String[] args) {
        ListNode head = LinkedListUtils.create(1, 2, 2, 2, 3, 3, 3, 3, 4, 5);
        Issue082 sol = new Issue082();
        sol.deleteDuplicates(head);
        System.out.println(LinkedListUtils.print(head));
    }
}
